package edu.jamestang.yeb.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import edu.jamestang.yeb.entity.Employee;
import edu.jamestang.yeb.entity.Salary;
import edu.jamestang.yeb.service.EmployeeService;
import edu.jamestang.yeb.service.SalaryService;
import edu.jamestang.yeb.util.PageResp;
import edu.jamestang.yeb.util.Resp;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author JamesTang
 * @date 2021/12/29 17:17
 */
@RestController
@RequestMapping("/salary/sobCfg")
public class SalarySobCfgController {

    @Autowired
    private SalaryService salaryService;

    @Autowired
    private EmployeeService employeeService;

    @ApiOperation(value = "Get all salaries")
    @GetMapping("/salaries")
    public List<Salary> getAllSalaries(){
        return salaryService.list();
    }

    @ApiOperation(value = "Get employee's Salary")
    @GetMapping("/")
    public PageResp getEmployeeWithSalary(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "10") Integer size){

        return employeeService.getEmployeeWithSalary(currentPage, size);
    }

    @ApiOperation(value = "Update Employee Salary")
    @PutMapping("/")
    public Resp updateEmployeeSalary(Integer eid, Integer sid){

        if(employeeService.update(new UpdateWrapper<Employee>().set("salaryId",sid).eq("id",eid))){
            return Resp.success("Update successful");
        }
        return Resp.error("Update failure");
    }
}
